<?php
//获取附属模型所有表单元素
function getAttachedModuleAll($module_name,$solevar){
	$module    = M("Module");
	$row = $module->where("module_name='$module_name'")->find();
	$html = '';
	
	//查附属模型数据
	$adata = M(ucfirst($module_name))->where("solevar='$solevar'")->find();

	
	if($row['id']){
		$id    = $row['id'];
		$where = "module_id =".$id;
		$Module_filed = M('Module_filed');
		$Module_filed_data = $Module_filed->where($where)->order('order_num DESC')->select();
		if($Module_filed_data){
			foreach($Module_filed_data as $k=>$v){
				$filed_attr = json_decode($v['filed_attr'],true);
				
				$valid_attr = json_decode($v['valid_attr'],true);
				foreach($valid_attr['tis'] as $vk=>$vv){
					$valid_attr['tis'][$vk] =  json_decode($vv,true);
				}
				
				$form['ele'][$k]['file_base']['filed_title'] = json_decode($v['filed_title'],true);
				$form['ele'][$k]['file_base']['css_class'] = $v['css_class'];
				$form['ele'][$k]['file_base']['filed_type'] = $v['filed_type'];
				$form['ele'][$k]['file_base']['tab_id'] = $v['tab_id'];
				$form['ele'][$k]['file_base']['filed_name'] = $v['filed_name'];
				
				$form['ele'][$k]['filed_attr'] = $filed_attr;
				$form['ele'][$k]['filed_attr']['default'] = @$adata[$v['filed_name']];
				$form['ele'][$k]['valid_attr'] = $valid_attr;
			}
		}
		
		import('ORG.Module.ThinkFrom');
		$form_obj = new ThinkForm($form);
		$html = $form_obj->createAttached();
	}
	return $html;
}

//获取虚拟模型单个字段对应的表单元素
function getVirtualModuleOne($filed_name,$filed_type,$dict_key,$default=''){
	$html = '';
	if($filed_type=='radio' || $filed_type=='checkbox' || $filed_type=='select' || $filed_type=='linkage'){
		
		
		$form['ele'][0]['filed_attr']['default'] = $default;
		$form['ele'][0]['file_base']['filed_type'] = $filed_type;
		$form['ele'][0]['file_base']['filed_name'] = $filed_name;
		$form['ele'][0]['filed_attr']['options'] = $dict_key;
		
		import('ORG.Module.ThinkFrom');
		$form_obj = new ThinkForm($form);
		$html .= $form_obj->createPureElement($form['ele'][0]);
	}
	return $html;
}

//获取单选、复选、联动对应的词典原生数组（数据库中存储的形态）
function get_module_filed_array($module_name,$filed_name){
	$result = '';
	
	//通过module_name查找该模型的id
	$module = M("Module");
	$row = $module->where("module_name='$module_name'")->find();
	$module_id = $row['id'];
	
	//通过module_id和filed_name在module_filed查出该字段对应的词典id
	$module_filed = M("Module_filed");
	$row_filed = $module->where("module_id='$module_id' AND filed_name='$filed_name'")->find();
	$filed_type = $row_filed['filed_type'];
	if($filed_type=='radio' || $filed_type=='checkbox' || $filed_type=='select' || $filed_type=='linkage'){
		$filed_attr = json_decode($row_filed['filed_attr'],true);
		$dict_id = $filed_attr['options'];
		
		//通过dict_id获取dict_key
		$model_dict = D("Dict");
		$row_dict = $Model_dict->find($dict_id);
		$result = F('Dict_'.$data_dict['key']);
	}
	
	return $result;
}

//获取单选、复选、联动对应的词典一维数组
function get_module_filed_one_array($module_name,$filed_name){
	$result = '';
	$lang = getLang();
	$arr = get_module_filed_array($module_name,$filed_name);
	if(is_array($arr)){
		foreach($arr as $k=>$v){
			$value = json_decode($v['value'],true);
			$result[$v['id']] = $value[$lang];
		}
	}
	
	return $result;
}

//根据缓存key生成对应表单元素
function getSelectCacheKey($key,$field = array("id","title"),$lang=false,$lanset='zh-cn',$nid=''){
	
	$list   = F("$key");
	$select = "";
	foreach($list as $k=>$v){
		if($lang){
			$title = dejsonByLang($v[$field[1]],$lanset);
		}else{
			$title = $v[$field[1]];
		}
		$id = $v[$field[0]];
		if($id == $nid){
			$select .= "<option value='".$id."' selected>".$title."</option>";
		}else{
			$select .= "<option value='".$id."'>".$title."</option>";
		}
	}
	
	return $select;
}



//某个字段是数据词典联动类型，根据其值，获取该联动数组所有前辈级的数组
